from django.contrib import admin
from .models import DeviceBrand, DeviceType, Device, PlatformCredential, DeviceCommand


@admin.register(DeviceBrand)
class DeviceBrandAdmin(admin.ModelAdmin):
    """设备品牌管理"""
    list_display = ['name', 'is_active', 'created_at']
    list_filter = ['is_active', 'created_at']
    search_fields = ['name']
    ordering = ['name']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('name', 'logo', 'is_active')
        }),
        ('时间信息', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )
    readonly_fields = ['created_at', 'updated_at']


@admin.register(DeviceType)
class DeviceTypeAdmin(admin.ModelAdmin):
    """设备类型管理"""
    list_display = ['name', 'brand', 'category', 'is_active', 'created_at']
    list_filter = ['brand', 'category', 'is_active', 'created_at']
    search_fields = ['name']
    ordering = ['brand', 'name']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('name', 'category', 'is_active')
        }),
        ('技术规格', {
            'fields': ('supported_protocols', 'default_commands'),
            'classes': ('collapse',)
        }),
        ('时间信息', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )
    readonly_fields = ['created_at', 'updated_at']


@admin.register(Device)
class DeviceAdmin(admin.ModelAdmin):
    """设备管理"""
    list_display = ['name', 'brand', 'device_type', 'room', 'connection_status', 'is_active', 'created_at']
    list_filter = ['brand', 'device_type', 'room', 'connection_status', 'is_active', 'created_at']
    search_fields = ['name', 'device_id']
    def brand_name(self, obj):
        """获取品牌名称"""
        return obj.brand.name
    brand_name.short_description = '品牌名称'
    
    ordering = ['-created_at']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('name', 'device_type', 'device_id', 'room')
        }),
        ('连接信息', {
            'fields': ('connection_status', 'status', 'last_seen')
        }),
        ('配置信息', {
            'fields': ('status', 'is_active'),
            'classes': ('collapse',)
        }),
        ('时间信息', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )
    readonly_fields = ['created_at', 'updated_at', 'last_seen']


@admin.register(PlatformCredential)
class PlatformCredentialAdmin(admin.ModelAdmin):
    """平台凭证管理"""
    list_display = ['brand_name', 'platform_username', 'is_active', 'token_expires_at', 'created_at']
    list_filter = ['brand', 'is_active', 'created_at']
    search_fields = ['brand__name', 'platform_username']
    def brand_name(self, obj):
        """获取品牌名称"""
        return obj.brand.name
    brand_name.short_description = '品牌名称'
    
    fieldsets = (
        ('平台信息', {
            'fields': ('brand', 'platform_username', 'is_active')
        }),
        ('认证信息', {
            'fields': ('platform_password', 'access_token', 'refresh_token', 'token_expires_at'),
            'classes': ('collapse',)
        }),
        ('配置信息', {
            'fields': ('additional_data',),
            'classes': ('collapse',)
        }),
        ('时间信息', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )
    readonly_fields = ['created_at', 'updated_at']
    
    def get_readonly_fields(self, request, obj=None):
        """加密密码字段只读"""
        readonly_fields = list(self.readonly_fields)
        if obj:  # 编辑时密码字段只读
            readonly_fields.append('platform_password')
        return readonly_fields


@admin.register(DeviceCommand)
class DeviceCommandAdmin(admin.ModelAdmin):
    """设备命令管理"""
    list_display = ['device', 'command', 'status', 'executed_at', 'created_at']
    list_filter = ['status', 'executed_at', 'created_at']
    search_fields = ['device__name', 'command']
    ordering = ['-created_at']
    
    fieldsets = (
        ('命令信息', {
            'fields': ('device', 'command', 'parameters', 'status')
        }),
        ('执行结果', {
            'fields': ('result', 'error_message', 'executed_at'),
            'classes': ('collapse',)
        }),
        ('时间信息', {
            'fields': ('created_at',),
            'classes': ('collapse',)
        }),
    )
    readonly_fields = ['created_at', 'executed_at']
